<?php

//echo br(3); ###########################################################################################

	$TEMP->set_file(array('handle' => DIR_VIEW_DEAN . 'subject.html'));
	
	$CID 	= _GET('cid');
	$LASTID = _GET('s');
	
	$semesterLinker = $DSC->getSemesterLinker($CID);

	//r_print($semesterLinker);
	# LSITING
	if ( !empty($semesterLinker) ) {
		$TEMP->set_block('handle',  'semester', 'semesterRow');
		foreach($semesterLinker as $semesterLinkerField => $semesterLinkerValue) {
			//r_print($semesterLinkerValue);
			$semDesc 	 = $semesterLinkerValue['sem_desc'];
			$semLinkerID = $semesterLinkerValue['sem_linker_idPK'];
			
			$semNote	 = (!empty($semesterLinkerValue['note'])) ? '*NOTE: ' . $semesterLinkerValue['note'] : '';
			
			$subjectListing = '';
			
			$subjectListingSemLinker = $DSC->getSubjectListingSemLinker($semLinkerID, $activeYear);
			
			if( !empty($subjectListingSemLinker) && $semesterLinkerValue['is_deleted'] == 'N' ) {
				$totalHRSTotal 		= 0;
				$totalHRSTotalSub 	= 0;
				$totalUnits 		= 0;
				$totalHRSTotalSub2	= '';
				$ifMinusThree		= 0;
				foreach($subjectListingSemLinker as $subjectListingSemLinkerValue) {
					
					$total_units 			= $subjectListingSemLinkerValue['total_units'];
					
					$total_units_parenthesis = ($total_units < 0) ? '(' . abs($total_units) . ')' : $total_units;
					
					$coChar  = (!empty($subjectListingSemLinkerValue['co_req'])) ? $DSC->getReqSubjectID($subjectListingSemLinkerValue['co_req']) : 'None';
					$preChar = (!empty($subjectListingSemLinkerValue['pre_req'])) ? $DSC->getReqSubjectID($subjectListingSemLinkerValue['pre_req']) : 'None';
					
					$totalHRS = $subjectListingSemLinkerValue['total_hrs'];
					
					$subjDesc = $subjectListingSemLinkerValue['subj_desc'];
					
					#LISTING
					
					$edButton = '<div class="icon-button">
									<table width="0" border="0" cellspacing="0" cellpadding="0">
										<tr align="left" valign="top">
											<td width="19">
												&nbsp;<a href="?page=NewSubject&slid=' . $semLinkerID . '&sid=' . $subjectListingSemLinkerValue['subj_idPK'] . '&hash=' . hashrandom($subjectListingSemLinkerValue['subj_idPK'] . $subjectListingSemLinkerValue['subj_code']) . '"><img src="' . DIR_IMAGES . 'icon/pencil.png" border="0" title="Edit" class="cursor-pointer"/></a>
											</td>
											<td width="19">
												<img src="' . DIR_IMAGES . 'icon/recyclebin.png" title="Delete" border="0" onclick="popupDelete(\'' . $subjectListingSemLinkerValue['subj_idPK'] . '\', \'200\');" class="cursor-pointer"/>
												<div class="delete-button" id="delete_button_' . $subjectListingSemLinkerValue['subj_idPK'] . '">
													<div class="delete-label">Delete this subject?</div>
													<div class="delete-yes-no" onclick="deleteAccount(\'' . $subjectListingSemLinkerValue['subj_idPK'] . '\', \'deleteSubject\');">Yes</div>
													<div class="delete-yes-no" onclick="popupDeleteHide(\'' . $subjectListingSemLinkerValue['subj_idPK'] . '\');">No</div>
												</div>
											</td>
											<td width="242">' . $subjDesc . '</td>
										</tr>
									</table>
								 </div>';
							
					$lastInsert = ($subjectListingSemLinkerValue['subj_idPK'] == $LASTID) ? 'active-light' : '';
							
					$subjectListing .= '<tr align="center" class="hover-light ' . $lastInsert . '">
											<td class="subject-td-bot" align="left">' . $edButton . '</td>
											<td class="subject-td-bot">' . $subjectListingSemLinkerValue['subj_code'] 	. '</td>
											<td class="subject-td-bot">' . $total_units_parenthesis 	. '</td>
											<td class="subject-td-bot">' . $subjectListingSemLinkerValue['lec_units'] 	. '</td>
											<td class="subject-td-bot">' . $subjectListingSemLinkerValue['lab_units'] 	. '</td>
											<td class="subject-td-bot">' . $subjectListingSemLinkerValue['lec_lab_units'] . '</td>
											<td class="subject-td-bot">' . $coChar 		. '</td>
											<td class="subject-td-bot">' . $preChar 	. '</td>
											<td class="subject-td-bot">' . $totalHRS 	. '</td>
										</tr>';
					if ($total_units > 0) {
						$totalUnits = $totalUnits + $total_units;
					}
					$totalHRSTotal = $totalHRSTotal + $totalHRS;
					
					if ( $total_units < 0 ) {
						$ifMinusThree++;
					}
					
					//echo "<br /><br />".$totalHRSTotal.' + '.$total_units.'='.$totalHRSTotalSub;
					
				}
				#TOTAL
				if ( $semesterLinkerValue['sem_idFK'] == 1 ) {
					
					if( $ifMinusThree > 0 ) {
						$totalHRSTotalSub2 = ((int)$totalHRSTotal - 3) . '/';
					}/*
					
					$totalHRSTotalSub = $totalHRSTotalSub + abs($total_units);
					
					if ( $totalHRSTotalSub != $totalHRSTotal ) {
						$totalHRSTotalSub2 = $totalHRSTotalSub . '/';
					}
					else {
						$totalHRSTotalSub2 = '';
					}*/
				}
					
				//r_print($semesterLinkerValue['sem_idFK']);
				$subjectListing .= '<tr align="center">
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"><strong>' . $totalUnits . '</strong></td>
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"></td>
										<td class="subject-td-bot"><strong>' . $totalHRSTotalSub2 . $totalHRSTotal . '</strong></td>
									</tr>';
				
			}
			$TEMP->set_var(array('sem_desc' 			=> '<div class="subj-year-semester-value">' . $semDesc . '</div>',
								 'add_subject_button'	=> '<a href="?page=NewSubject&slid=' . $semLinkerID . '&hash=' . hashRandom($dept_course.$course_desc.$based.$year) . '"><div class="subj-year-semester-button" title="' . $semDesc . '">+ Add Subject</div></a>',
								 'sem_linker_id' 		=> $semLinkerID,
								 'sem_note'				=> $semNote,
								 'subject_listing' 		=> $subjectListing));
			$TEMP->parse('semesterRow', 'semester', true);
		}
	}
	else {
		$TEMP->set_var(array('sem_desc' 			=> '',
							 'add_subject_button' 	=> '',
							 'sem_note'				=> '',
							 'sem_linker_id' 		=> '',
							 'subject_listing'		=> ''));
	}
	
	##################### CHECKLIST HEADER #####################
	
		$courseHeader = $DSC->getCourseHeader($strAccDept);
		$getCourse 	  = $DSC->getCourse($CID);
	
		#SET Course
		if(!empty($getCourse)) {
			$course_label	= $getCourse['course_desc'] . ' (' . $getCourse['course_code'] . ')';
			$course_title	= $course_label;
		}
		else {
			$course_label	= '______________________________________________';
			$course_title	= '&nbsp;';
		}
		
		#SET Checklist Header
		if(!empty($getCourse)) {
			$based 		 	= $courseHeader['head_based'];
			$year 		 	= $courseHeader['head_year'];
		}
		else {
			$based 		 	= '__________________________________';
			$year 		 	= '_______________';
		}
	
	##################### CHECKLIST HEADER #####################
	
	$TEMP->set_var(array('search' 			=> $search,
						 'dean_sub_menu'	=> $pageLib->deanSubMenu(DIR_IMAGES),
						 'dept_course'		=> $strAccCourse,
						 'course_desc'		=> $course_label,
						 'course_title'		=> $course_title,
						 'course_id'		=> $CID,
						 'based'			=> $based,
						 'year'				=> $year
						));
?>